<?php

namespace App\Store\Services;

use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Concerns\WithStyles;
use Maatwebsite\Excel\Concerns\FromCollection;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;

class BussinessSummaryExcelExport implements FromCollection,WithStyles
{

    //公共导出
    private $row1;
    private $data1;
    private $row2;
    private $data2;
    private $row3;
    private $data3;
    private $row4;
    private $data4;
    private $row5;
    private $data5;
    private $datawith;
    private $row_count=0;


    public function __construct(array $row1, array $data1,array $row2, array $data2,array $row3, array $data3,array $row4, array $data4,array $row5=[], array $data5=[] ,array $datawith = [])
    {
        $this->row1 = $row1;
        $this->data1 = $data1;
        $this->row2 = $row2;
        $this->data2 = $data2;
        $this->row3 = $row3;
        $this->data3 = $data3;
        $this->row4 = $row4;
        $this->data4 = $data4;

        $this->row5 = $row5;
        $this->data5 = $data5;
        $this->datawith = $datawith;

    }

    /**
     * @return \Illuminate\Support\Collection
     */
    public function collection()
    {
        $row1 = $this->row1;
        $data1 = $this->data1;
        $row2 = $this->row2;
        $data2 = $this->data2;
        $row3 = $this->row3;
        $data3 = $this->data3;
        $row4 = $this->row4;
        $data4 = $this->data4;

        $row5 = $this->row5;
        $data5 = $this->data5;
        $row_new=[];
        //设置表头
        foreach ($row1[2] as $key => $value) {
            $key_arr1[] = $key;
        }
        array_push($row_new, $row1[0]);
        array_push($row_new, $row1[1]);
        array_push($row_new, $row1[2]);
        //输入数据
        foreach ($data1 as $key => &$value) {
            $js = [];
            for ($i = 0; $i < count($key_arr1); $i++) {
                $js = array_merge($js, [$key_arr1[$i] => $value[$key_arr1[$i]] ?? '']);
            }
            array_push($row_new, $js);
            unset($value);
        }

        array_push($row_new, ['']);
        array_push($row_new, ['']);

        //设置表头
        foreach ($row2[0] as $key => $value) {
            $key_arr2[] = $key;
        }
        array_push($row_new, $row2[0]);
        //输入数据
        foreach ($data2 as $key => &$value) {
            $js = [];
            for ($i = 0; $i < count($key_arr2); $i++) {
                $js = array_merge($js, [$key_arr2[$i] => $value[$key_arr2[$i]] ?? '']);
            }
            array_push($row_new, $js);
            unset($value);
        }


        array_push($row_new, ['']);
        array_push($row_new, ['']);
        array_push($row_new, ['纯收旧料','','','','','兑换旧料','','','','']);


        //设置表头
        foreach ($row3[0] as $key => $value) {
            $key_arr3[] = $key;
        }
        array_push($row_new, $row3[0]);
        //输入数据
        foreach ($data3 as $key => &$value) {
            $js = [];
            for ($i = 0; $i < count($key_arr3); $i++) {
                $js = array_merge($js, [$key_arr3[$i] => $value[$key_arr3[$i]] ?? '']);
            }
            array_push($row_new, $js);
            unset($value);
        }


        array_push($row_new, ['']);
        array_push($row_new, ['']);


        //设置表头
        foreach ($row4[0] as $key => $value) {
            $key_arr4[] = $key;
        }
        array_push($row_new, $row4[0]);
        //输入数据
        foreach ($data4 as $key => &$value) {
            $js = [];
            for ($i = 0; $i < count($key_arr4); $i++) {
                $js = array_merge($js, [$key_arr4[$i] => $value[$key_arr4[$i]] ?? '']);
            }
            array_push($row_new, $js);
            unset($value);
        }

        // array_push($row_new, ['']);
        // array_push($row_new, ['']);

        // array_push($row_new, ['营业汇总表（明细）']);

        // //设置表头
        // foreach ($row5[0] as $key => $value) {
        //     $key_arr5[] = $key;
        // }
        // array_push($row_new, $row5[0]);
        // //输入数据
        // foreach ($data5 as $key => &$value) {
        //     $js = [];
        //     for ($i = 0; $i < count($key_arr5); $i++) {
        //         $js = array_merge($js, [$key_arr5[$i] => $value[$key_arr5[$i]] ?? '']);
        //     }
        //     array_push($row_new, $js);
        //     unset($value);
        // }




        return collect($row_new);
    }

    public function styles(Worksheet $sheet)
    {


        $sheet->mergeCells('A1:M1');
        $sheet->mergeCells('A2:M2');

        $sheet->getStyle('A1:M1')->applyFromArray(['alignment' => ['horizontal' => 'left']]);
        $sheet->getStyle('A1:M1')->getFont()->setSize(20);
        $sheet->getRowDimension('1')->setRowHeight(40);
        $sheet->getStyle('A1:M1')->getAlignment()->setVertical('center');
        $sheet->getStyle('A1')->getFont()->setBold(true);

        //$sheet->getStyle('A2:M2')->applyFromArray(['alignment' => ['horizontal' => 'center']]);
        $sheet->getStyle('A2:M2')->getFont()->setSize(20);
        $sheet->getRowDimension('1')->setRowHeight(40);
       // $sheet->getStyle('A2:M2')->getAlignment()->setVertical('center');
        $sheet->getStyle('A2')->getFont()->setBold(true);

        if (count($this->datawith)) {
            foreach ($this->datawith as $key => $val) {
                $sheet->getColumnDimension($key)->setWidth($val);
            }
        }
        $count=count($this->row1)+count($this->data1)+count($this->row2)+count($this->data2)+4;

        //设置不适用科学计数法
        $sheet->getStyle('A')
            ->getNumberFormat()
            ->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_NUMBER);

        //第三个表头
        $sheet->mergeCells('A'.($count+1).':E'.($count+1));
        $sheet->mergeCells('F'.($count+1).':J'.($count+1));
        $sheet->getStyle('A'.($count+1).':J'.($count+1))->applyFromArray(['alignment' => ['horizontal' => 'center']]);
        $sheet->getStyle('A'.($count+1).':J'.($count+1))->getFont()->setSize(14);
        $sheet->getRowDimension(($count+1))->setRowHeight(25);
        $sheet->getStyle('A'.($count+1).':J'.($count+1))->getFill()
            ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
            ->getStartColor()->setRGB('C5D9F1');

        //第一个表头
        $sheet->getStyle('A2:M3')->getFont()->setSize(14);
        $sheet->getStyle('A3:M3')->applyFromArray(['alignment' => ['horizontal' => 'center']]);
       
        $sheet->getStyle('A3:M3')->getFill()
            ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
            ->getStartColor()->setRGB('C5D9F1');

        //第二个表头
        $sheet->getStyle('A7:K7')->applyFromArray(['alignment' => ['horizontal' => 'center']]);
        $sheet->getStyle('A7:K7')->getFont()->setSize(14);
        $sheet->getRowDimension(5)->setRowHeight(25);
        $sheet->getStyle('A7:K7')->getFill()
            ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
            ->getStartColor()->setRGB('C5D9F1');

        //第4个表头
        $count3=count($this->row1)+count($this->data1)+2+count($this->row2)+count($this->data2)+2+count($this->row3)+count($this->data3)+3;

        $sheet->getStyle('A'.($count3+1).':D'.($count3+1))->applyFromArray(['alignment' => ['horizontal' => 'center']]);
        $sheet->getStyle('A'.($count3+1).':D'.($count3+1))->getFont()->setSize(14);
        $sheet->getRowDimension(($count3+1))->setRowHeight(25);
        $sheet->getStyle('A'.($count3+1).':D'.($count3+1))->getFill()
            ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
            ->getStartColor()->setRGB('C5D9F1');

        $sheet->getStyle('A1:M'.(count($this->row1)+count($this->data1)))->applyFromArray([
            'borders' => [
                'allBorders' => [
                    'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
                ],
            ],
        ]);

       $sheet->getStyle('A'.(count($this->row1)+count($this->data1)+2+1).':K'.(count($this->row1)+count($this->data1)+2+count($this->row2)+count($this->data2)))->applyFromArray([
            'borders' => [
                'allBorders' => [
                    'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
                ],
            ],
        ]);

        $sheet->getStyle('A'.(count($this->row1)+count($this->data1)+2+count($this->row2)+count($this->data2)+2+1).':J'.(count($this->row1)+count($this->data1)+2+count($this->row2)+count($this->data2)+6))->applyFromArray([
            'borders' => [
                'allBorders' => [
                    'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
                ],
            ],
        ]);
        $sheet->getStyle('A'.(count($this->row1)+count($this->data1)+2+count($this->row2)+count($this->data2)+6+2+1).':D'.(count($this->row1)+count($this->data1)+2+count($this->row2)+count($this->data2)+6+2+count($this->row4)+count($this->data4)))->applyFromArray([
            'borders' => [
                'allBorders' => [
                    'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
                ],
            ],
        ]);

        $count=count($this->row1)+count($this->data1)+2+count($this->row2)+count($this->data2)+6+2+count($this->row4)+count($this->data4);

        //设置居中
        $sheet->getStyle('A1:Z'.$count+count($this->data5)+count($this->row5)+100)
            ->getAlignment()
            ->setVertical(Alignment::HORIZONTAL_CENTER);
        $sheet->getStyle('A1:Z'.$count+count($this->data5)+count($this->row5)+100)
            ->applyFromArray(['alignment' => ['horizontal' => Alignment::HORIZONTAL_CENTER]]);
        //设置特殊要求的行  
        $sheet->getStyle('A2:M2')->getAlignment()->setHorizontal('left');

    }


}
